package com.zxy.study.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;

import com.zxy.study.R;


/**
 * BlueBaseDialog
 *
 * @author : Brice.Zhang
 * @date : 2019/12/3
 */
public class BlueBaseDialog extends Dialog {

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    public BlueBaseDialog(@NonNull Context context) {
        super(context, R.style.BaseDialog);
    }

    public BlueBaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    public void show() {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            safeShow();
        } else {
            mHandler.post(this::safeShow);
        }
    }

    @Override
    public void dismiss() {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            safeDismiss();
        } else {
            mHandler.post(this::safeDismiss);
        }
    }

    private void safeShow() {
        try {
            super.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void safeDismiss() {
        try {
            super.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
